const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');

const entry = {
  main: resolve(__dirname, 'src', 'index.js'),
  second: resolve(__dirname, 'src', 'second.js'),
};

const output = {
  path: resolve(__dirname, 'build'),
  filename: '[name].js',
  clean: true,
  assetModuleFilename: 'assets/[name][ext]',
};

const plugins = [
  new HtmlWebpackPlugin({
    template: resolve(__dirname, 'public', 'index.html'),
  }),
  new MiniCssExtractPlugin({
    filename: 'style/[name].css',
  }),
  new ESLintPlugin(),
];

const MODULE = {
  rules: [
    {
      test: /\.css$/, // 匹配以.css结尾的文件
      use: [MiniCssExtractPlugin.loader, 'css-loader'], // 使用 css-loader 处理
    },
    {
      test: /\.(png|jpeg|jpg)$/i,
      type: 'asset/resource',
      generator: {
        filename: 'assets/image/[name][ext]',
      },
    },
    {
      test: /.(js)$/,
      exclude: /node_modules/,
      use: [{
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env'],
          plugins: [
            [
              '@babel/plugin-transform-runtime',
            ],
          ],
        },
      }],
    },
  ],
};

module.exports = {
  entry,
  output,
  plugins,
  module: MODULE,
  mode: 'development',
};
